<route lang="json5">
{
  style: {
    navigationBarTitleText: '我是谁'
  }
}
</route>

<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { useSupabaseCoupleStore } from '@/store/supabaseCouple'

defineOptions({ name: 'WhoAmI' })

const coupleStore = useSupabaseCoupleStore()
const loading = ref(true)
const user1Name = ref('TaA')
const user2Name = ref('TaB')

onMounted(async () => {
  if (!coupleStore.profile) {
    await coupleStore.initProfile()
  }
  if (coupleStore.profile) {
    user1Name.value = coupleStore.profile.user1.name || 'TaA'
    user2Name.value = coupleStore.profile.user2.name || 'TaB'
  }
  loading.value = false
})

function selectRole(role: 'user1' | 'user2') {
  try {
    uni.setStorageSync('whoAmI', role)
    const uid = role === 'user1' ? (coupleStore.profile?.user1.id || '') : (coupleStore.profile?.user2.id || '')
    if (uid)
      uni.setStorageSync('whoAmI_userId', uid)
    uni.showToast({ title: '已设置', icon: 'success' })
    setTimeout(() => uni.navigateBack(), 500)
  }
  catch {
    uni.showToast({ title: '设置失败', icon: 'none' })
  }
}
</script>

<template>
  <view class="who-container">
    <view class="glass-card" :class="{ fadein: !loading }">
      <view class="title">
        请选择你的身份
      </view>
      <view class="subtitle">
        用于标记发布动态时的身份
      </view>

      <view class="role-grid">
        <view class="role-card bounce" @click="selectRole('user1')">
          <view class="avatar pink">
            A
          </view>
          <view class="name">
            {{ user1Name }}
          </view>
          <view class="tag">
            我是 Ta
          </view>
        </view>
        <view class="role-card bounce delay" @click="selectRole('user2')">
          <view class="avatar blue">
            B
          </view>
          <view class="name">
            {{ user2Name }}
          </view>
          <view class="tag">
            我是 Ta
          </view>
        </view>
      </view>
    </view>
  </view>
  <wd-toast />
  <wd-loading :loading="loading" />
</template>

<style scoped lang="scss">
.who-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #ffb6c1 0%, #ffc0cb 50%, #ffb6c1 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px 16px;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    inset: 0;
    background-image:
      radial-gradient(circle at 25% 25%, rgba(255, 182, 193, 0.35) 0%, transparent 50%),
      radial-gradient(circle at 75% 75%, rgba(255, 192, 203, 0.35) 0%, transparent 50%);
    pointer-events: none;
  }
}

.glass-card {
  width: 100%;
  max-width: 560px;
  background: rgba(255, 255, 255, 0.92);
  border-radius: 16px;
  box-shadow: 0 12px 32px rgba(255, 105, 180, 0.2);
  padding: 20px;
  transform: translateY(12px);
  opacity: 0;
  transition: all 0.4s ease;

  &.fadein {
    transform: translateY(0);
    opacity: 1;
  }
}

.title {
  font-size: 20px;
  font-weight: 700;
  color: #333;
}
.subtitle {
  margin-top: 6px;
  font-size: 12px;
  color: #777;
}

.role-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-top: 16px;
}

.role-card {
  background: #fff;
  border-radius: 12px;
  padding: 16px;
  text-align: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.06);
  transition:
    transform 0.2s ease,
    box-shadow 0.2s ease;
}

.avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  margin: 0 auto 8px auto;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 20px;
}
.avatar.pink {
  background: linear-gradient(135deg, #ff69b4, #ff1493);
}
.avatar.blue {
  background: linear-gradient(135deg, #45b7d1, #4ecdc4);
}

.name {
  font-size: 14px;
  color: #333;
  font-weight: 600;
}
.tag {
  font-size: 12px;
  color: #999;
  margin-top: 4px;
}

.role-card:active {
  transform: scale(0.98);
}

/* 轻微弹跳动画 */
.bounce {
  animation: bounceIn 0.6s ease both;
}
.bounce.delay {
  animation-delay: 0.12s;
}
@keyframes bounceIn {
  0% {
    transform: scale(0.9);
    opacity: 0;
  }
  60% {
    transform: scale(1.05);
    opacity: 1;
  }
  100% {
    transform: scale(1);
  }
}
</style>
